﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Gmantis.Controls.Chart
{
    public class RPolygon : Symbol
    {
        // Fields
        public static readonly DependencyProperty NumVerticesProperty = Utils.RegisterProperty("NumVertices", typeof(int), typeof(RPolygon), new PropertyChangedCallback(RPolygon.OnNumVerticesChanged), 3);

        // Methods
        public RPolygon()
        {
            PathGeometry geometry = new PathGeometry
            {
                Figures = new PathFigureCollection()
            };
            base.geometry = geometry;
            base.Data = base.geometry;
            this.NumVertices = 3;
        }

        protected void AddFakeEllipse(PathGeometry pg, Point center, double rx, double ry, double w2)
        {
            EllipseGeometry geometry = new EllipseGeometry
            {
                Center = center,
                RadiusX = rx + w2,
                RadiusY = ry + w2
            };
        }

        internal override object Clone()
        {
            RPolygon clone = new RPolygon();
            base.CloneAttributes(clone);
            clone.NumVertices = this.NumVertices;
            return clone;
        }

        private static void OnNumVerticesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            RPolygon polygon = (RPolygon)obj;
            polygon.UpdateGeometry(null, polygon.Size);
        }

        protected override void UpdateGeometry(PathGeometry pg, Size sz)
        {
            if (pg == null)
            {
                pg = (PathGeometry)base.geometry;
            }
            if (sz.IsEmpty)
            {
                sz = this.Size;
            }
            pg.Figures.Clear();
            double rx = 0.5 * sz.Width;
            double ry = 0.5 * sz.Height;
            double num3 = 0.5 * base.StrokeThickness;
            Point center = new Point(rx + num3, ry + num3);
            double numVertices = this.NumVertices;
            PathFigure figure = new PathFigure
            {
                StartPoint = new Point(center.X + rx, center.Y),
                IsClosed = true
            };
            for (int i = 1; i < numVertices; i++)
            {
                double d = ((((double)i) / numVertices) * 2.0) * 3.1415926535897931;
                double x = center.X + (rx * Math.Cos(d));
                double y = center.Y + (ry * Math.Sin(d));
                LineSegment segment = new LineSegment
                {
                    Point = new Point(x, y)
                };
                figure.Segments.Add(segment);
            }
            this.AddFakeEllipse(pg, center, rx, ry, num3);
            pg.Figures.Add(figure);
            Canvas.SetLeft(this, (this.symCenter.X - rx) - num3);
            Canvas.SetTop(this, (this.symCenter.Y - ry) - num3);
        }

        // Properties
        protected override Shape LegendShape
        {
            get
            {
                Path path = new Path
                {
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                PathGeometry pg = new PathGeometry();
                Size sz = this.Size;
                if (sz.Width > 16.0)
                {
                    sz.Width = 16.0;
                }
                if (sz.Height > 16.0)
                {
                    sz.Height = 16.0;
                }
                this.UpdateGeometry(pg, sz);
                path.Data = pg;
                path.RenderTransform = base.RenderTransform;
                path.RenderTransformOrigin = base.RenderTransformOrigin;
                return path;
            }
        }

        public int NumVertices
        {
            get
            {
                return (int)base.GetValue(NumVerticesProperty);
            }
            set
            {
                base.SetValue(NumVerticesProperty, value);
            }
        }
    }
}
